package com.hdoit.ares.web.tp.zzb.controllers;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.app.service.IAppRemindMessageService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.tp.zzb.model.Tomeasure;
import com.hdoit.ares.logical.tp.zzb.service.ITomeasureService;
import com.hdoit.ares.logical.viewmodel.tp.zzp.TomeasureSearch;
import com.hdoit.ares.selfcommon.enums.brokerage.ConfirmState;
import com.hdoit.ares.selfcommon.jpush.JpushClient;
import com.hdoit.ares.web.wechat.WechatTemplateManager;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      caochengmeng
 * @email       
 * @createtime  2018年03月01日 14:56:17
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/zzb/tomeasure/")
public class TomeasureController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/zzb/tomeasure/";
    }

    private ITomeasureService tomeasureService;
    public ITomeasureService getTomeasureService() {
        return tomeasureService;
    }
    @Resource
    public void setTomeasureService(ITomeasureService tomeasureService) {
        this.tomeasureService = tomeasureService;
    }
    
    private ICustomerService customerService;
    public ICustomerService getCustomerService() {
		return customerService;
	}
    @Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

    private IAppRemindMessageService appRemindMessageService;
    
	public IAppRemindMessageService getAppRemindMessageService() {
		return appRemindMessageService;
	}
	@Resource
	public void setAppRemindMessageService(
			IAppRemindMessageService appRemindMessageService) {
		this.appRemindMessageService = appRemindMessageService;
	}

	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="tomeasureDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String toConfirmViewName = "toConfirm";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,TomeasureSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(tomeasureService.queryCount(search));
        List<Tomeasure> tomeasures= tomeasureService.queryList(pagerModel,search);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("tomeasures", tomeasures);
        model.addAttribute("tomeasureSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String tomeasureDetailed(Model model,String key){
        Tomeasure tomeasure=new Tomeasure();
        if(!StringUtils.isEmpty(key)){
            tomeasure=tomeasureService.queryModel(key);
        }
        model.addAttribute("tomeasure", tomeasure);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Tomeasure tomeasure){
        if(StringUtils.isEmpty(tomeasure.getId())){
            tomeasure.setId(CommonUtils.getKeyId());
            tomeasure.setConfirmState(ConfirmState.unConfirm);
            tomeasure.setModelStatus(ModelStatus.add);
        }else{
        	tomeasure.setModifyTime(DateUtils.getCurrent());
        	tomeasure.setModifyUserId(getLoginModel().getLoginId());
        	tomeasure.setModifyUserName(getLoginModel().getLoginName());
            tomeasure.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(tomeasureService.applyChange(tomeasure)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("tomeasure", tomeasure);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(tomeasureService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @RequestMapping(value =toConfirmViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    @ResponseBody 
    public String toConfirm(Model model,String tomeasureId){
    	System.err.println("1111111");
    	System.err.println("key"+tomeasureId);
    	Tomeasure tomeasure = tomeasureService.queryModel(tomeasureId);
    	String insuName = tomeasure.getInsuName();
    	String carLicenseNo = tomeasure.getCarLicenseNo();
    	String totalMoney = tomeasure.getTotalMoney();
    	Date currentTime = DateUtils.getCurrent();
    	String custId = tomeasure.getCustomerId();
    	Customer customer = customerService.queryModel(custId);
        if(tomeasureService.updateTomeasure(tomeasureId,getLoginModel())){
        	System.err.println("22222222222");
        	WechatTemplateManager.sendTomeasure(insuName, carLicenseNo, totalMoney, currentTime, customer,tomeasureId);
        	//app发送测算信息通知
        	JpushClient.sendTomeasureApp(insuName, carLicenseNo, totalMoney, customer, tomeasureId,appRemindMessageService);
        	return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
}